﻿// Author: Jin Qing (http://blog.csdn.net/jq0123)

#include "server_mailbox_mgr.h"

#include "cfg_reader.h"  // for CCfgReader
#include "log.h"
#include "server_mailbox.h"  // for CServerMailBox
#include "server_type.h"  // for SERVER_MAILBOX_RESERVE_SIZE

const char LOG_NAME[] = "CServerMailBoxMgr";

CServerMailBoxMgr::CServerMailBoxMgr()
{
}

CServerMailBoxMgr::~CServerMailBoxMgr()
{
}

bool CServerMailBoxMgr::Init(CCfgReader& cfg)
{
	uint16_t nServerCount = SERVER_MAILBOX_RESERVE_SIZE;
	for (uint16_t nSvrId = 1; nSvrId < nServerCount; ++nSvrId)
	{
		std::string sServer = (Fmt("server_%u") % nSvrId).str();
		const std::string& strServerType = cfg.GetOptValue(sServer.c_str(), "type", "");
		if (strServerType.empty())
			continue;

		EServerType eServerType = ServerType::GetType(strServerType);
		if (eServerType != SERVER_BASE && eServerType != SERVER_CELL)
		{
			// base和cell可以配置多个,其他类型的服务器id必须和服务器类型编号一致
			if (static_cast<uint16_t>(eServerType) != nSvrId)
			{
				throw (Fmt("%s, server_id error,need=%d,set=%d") %
					strServerType % eServerType % nSvrId).str();
			}
		}

		CServerMailBox* mb = new CServerMailBox(nSvrId,
			cfg.GetValue(sServer.c_str(), "ip").c_str(),
			atoi(cfg.GetValue(sServer.c_str(), "port").c_str()));
		m_mbs.push_back(mb);
	}

	return true;
}
